package src.org.jess.serv;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;


public class ReceiveProcessor implements WebSocket.OnTextMessage{
		private  Connection connection;
		private	 ConnectionLst connectionLst = ConnectionLst.getSingle();
		private Map<String, Connection> socketsMap = connectionLst.getConnectionLst();
		private JsonUtil jsonUtil = new JsonUtil();
		private MessageVo messageVo = new MessageVo();
		private String userName;
		
		@Override
		public void onOpen(Connection connection) {
			System.out.println("i am in open");
			this.connection = connection;//保存连接
			System.out.println(this.connection.getMaxIdleTime());
			this.connection.setMaxIdleTime(3000000);
		}
		
		@Override
		public void onClose(int i, String s) {
			System.out.println("I am in close"+i);
			socketsMap.remove(this.userName);
			try {
				this.notifyOnline();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		
		@Override
		public void onMessage(String msg) {
				System.out.println(msg);
				System.out.println("i am in message");
				messageVo = (MessageVo) (jsonUtil.json2Obj(msg, MessageVo.class));
				System.out.println(messageVo.getType());
				switch (messageVo.getType()) {
				case MessageVo.TYPE_LOGIN://登录消息处理
					System.out.println(msg);
					this.doConnect(messageVo);
					break;
				case MessageVo.TYPE_GROUP://群发消息处理
					this.doGroupChat(messageVo);
					break;
				default://普通消息处理
					if(messageVo.getTo()!=null){
						try {
							System.out.println("from:"+messageVo.getFrom());
							System.out.println("to:"+messageVo.getTo());
							System.out.println(socketsMap.get(messageVo.getTo()));
							if(messageVo.getFrom().equals(messageVo.getTo())){
								System.out.println("the same");
								messageVo.setType(2);
							}
							socketsMap.get(messageVo.getTo()).sendMessage(jsonUtil.obj2Json(messageVo));
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					break;
				}
			}

		/*
		 * 群发消息
		 */
		private void doGroupChat(MessageVo messageVo2) {
			for(Entry<String,Connection> entry:connectionLst.getConnectionLst().entrySet()){
				System.out.println(jsonUtil.obj2Json(connectionLst));
				try {
					entry.getValue().sendMessage(jsonUtil.obj2Json(messageVo2));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}			
		}

		/*
		 * 建立连接
		 */
		private void doConnect(MessageVo messageVo) {
			try {
				MessageVo openMessageVo = new MessageVo();
				//检查是否已登录
				if(socketsMap.containsKey(messageVo.getFrom())){
						openMessageVo.setType(-2);
						this.connection.sendMessage(jsonUtil.obj2Json(openMessageVo));
						return;
					}
				this.userName = messageVo.getFrom();
				connectionLst.getConnectionLst().put(messageVo.getFrom(), connection);
				openMessageVo.setType(0);
				openMessageVo.setContent("success");
				this.connection.sendMessage(jsonUtil.obj2Json(messageVo));
				if(connectionLst.getConnectionLst()!=null){
					this.notifyOnline();
				}
				System.out.println("success connect");
			} catch (Exception e) {
			}
			
		}
		
		/*
		 * 通知有人上线
		 */
		private void notifyOnline() throws IOException {
			for(Entry<String,Connection> entry:connectionLst.getConnectionLst().entrySet()){
				System.out.println(jsonUtil.obj2Json(connectionLst));
				entry.getValue().sendMessage(jsonUtil.obj2Json(connectionLst.getConnectionLst().keySet()));
			}
		}
		

}
